﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.IO;
using System.Collections;
using System.Web.Mvc;
using MvcLibrary.Repository;

namespace WebVideo.Areas.Admin.Models
{
    public class UserModel
    {
        public string UserName { get; set; }       
        public string Name { get; set; }
        public int? YearBorn { get; set; }     
        public string Sex { get; set; }
        public string City { get; set; }
        public string Avatar {get; set;}
        public string Email {get; set;}
        public string BankAccount {get; set;}
        public string NameBank { get; set; }
        public string Link {get; set;}
        public string UserNameRoot { get; set; }
        public string Ip {get; set;}
        public int? PointVideo {get; set;}
        public int? PointFriend1 {get; set;}
        public int? PointFriend2 { get; set; }
        public int? Status {get; set;}
        public DateTime? Date { get; set; }


        public static IEnumerable<EntityUser> Index()
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityUsers
                        select p;
            return query.ToList();
        }
        public IEnumerable<EntityListVideoOfUser> DSVideo { get; set; }
        public IEnumerable<EntityReport_Point> DSPoint { get; set; }
        public static UserModel LayModel(string US)
        {
            UserModel Us = new UserModel();
            dbVideoDataContext db = new dbVideoDataContext();
            
            EntityUser tnew = db.EntityUsers.Where(p => p.UserName == US).Single();
            Us.UserName = tnew.UserName;
            Us.Name = tnew.Name;
            Us.YearBorn = tnew.YearBorn;
            Us.Sex = tnew.Sex;
            Us.City = tnew.City;
            Us.Avatar = tnew.Avatar;
            Us.Email = tnew.Email;
            Us.BankAccount = tnew.BankAccount;
            Us.NameBank = tnew.NameBank;
            Us.Link = tnew.Link;
            Us.UserNameRoot = tnew.UserNameRoot;
            Us.Ip = tnew.Ip;
            Us.PointVideo = tnew.PointVideo;
            Us.PointFriend1 = tnew.PointFriend1;
           Us.PointFriend2 = tnew.PointFriend2;
            Us.Status = tnew.Status;
            Us.Date = tnew.Date;

            var query = from p in db.EntityListVideoOfUsers
                        where p.UserName == tnew.UserName
                        select p;
            Us.DSVideo = query.ToList();

            var query1 = from p1 in db.EntityReport_Points
                        where p1.UserName == tnew.UserName
                        select p1;
            Us.DSPoint = query1.ToList();

            return Us;

        }
        public static void Ban(string Id, int l)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityUser tnew = db.EntityUsers.Where(p => p.UserName == Id).Single();
            tnew.Status = l;
            db.SubmitChanges();

        }
        public static void Delete(string Username)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityUser faq = db.EntityUsers.Where(p => p.UserName == Username).Single();
            db.EntityUsers.DeleteOnSubmit(faq);
            db.SubmitChanges();
        }
        public static void DeleteChat(string Username)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityChats
                        where p.Friend == Username
                        select p;
            db.EntityChats.DeleteAllOnSubmit(query.ToList());
            db.SubmitChanges();
        }
        public static void DeleteFriend(string Username)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityFriends
                        where p.UserNameFriend == Username
                        select p;
            db.EntityFriends.DeleteAllOnSubmit(query.ToList());
            db.SubmitChanges();
        }
        public static void DeleteUser(string Username)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityUsers
                        where p.UserNameRoot == Username
                        select p;
            foreach (var item in query )
            {
                item.UserNameRoot = null;
                db.SubmitChanges();
            }
                
        }
    }
}